<?php

if (!defined('BASEPATH'))
	exit('No direct script access allowed');
/**
 * Description of permission
 *
 * @author Admin
 */
class permission extends MY_Controller{
	
	function __construct() {
		parent::__construct();
		$this->load->model('model_permission');
	}

	public function index()
	{
		$this->data['temp'] = 'admin/permission/index';
		$this->load->view('admin/main',$this->data);
	}
	public function read()
	{
		// Phan trang
		$page = request('p',1);
		$pagination = pagination_init($this->model_permission->get_total(), $page);
		
		// Doc du lieu
		$permissions = $this->model_permission->get_list(array(
			'limit'	=> array($pagination->from,$pagination->limit)
		));
		
		echo json_encode(array(
			'state'	=> 'success',
			'data'	=> $permissions,
			'pagination'	=> $pagination
		));
	}
	
//	public function get_list()
//	{
//		echo json_encode(array(
//			'state'	=> 'success',
//			'data'	=> $this->model_permission->get_list(array(
//				'select'	=> 'id','name'
//			)),
//		));
//	}
	
	public function create()
	{
		$this->load->library('form_validation');
		
		// Rule
		$this->form_validation->set_rules('permission','lang:permission','required');
		
		$data = request_filter(array('permission','name','description'));
		if($this->form_validation->run()){
			$this->model_permission->create($data);
			echo json_encode(array(
				'state'	=> 'success',
				'title'	=> lang('notice'),
				'message'	=> lang('create_success'),
			));
		}
		else
		{
			echo json_encode(array(
				'state'	=> 'error',
				'title'	=> lang('notice'),
				'message'	=> form_error_message($data)
			));
		}
	}
	
	public function update()
	{
		$this->load->library('form_validation');
		$this->form_validation->set_rules('permission','lang:permission','required');
		$id = request('id');
		$data = request_filter(array('permission','name','description'));
		if($this->form_validation->run()){
			$this->model_permission->update($id,$data);
			echo json_encode(array(
				'state'	=> 'success',
				'message'	=> lang('update_success'),
				'title'		=> lang('notice'),
			));
		}
		else
		{
			json_encode(array(
				'state'	=> 'error',
				'title'		=> lang('error'),
				'message'	=> form_error_message($data),
			));
		}
	}
	public function delete()
	{
		$id = request('id');
		$this->model_permission->del($id);
		echo json_encode(array(
			'state'		=> 'success',
			'message'	=> lang('delete_success'),
			'title'		=> lang('notice'),
			'id'		=> $id,
		));
	}
	
	public function detail()
	{
		$id = request('id');
		$permission = $this->model_permission->get_info($id);
		if($permission!==false){
			if(strlen($permission->name)==0) $permission->name = $permission->permission;
			echo json_encode (array(
				'state'	=> 'success',
				'data'	=> $permission,
			));
		}
		else
			echo json_encode (array(
				'state'	=> 'error',
				'message'	=> 'permission_not_exist',
			));
	}
	public function update_all()
	{
		$this->load->library('controllerlist');
		// Lay cac method trong controller
		$controllers = $this->controllerlist->getControllers();
		foreach($controllers as $controller => $methods)
			foreach($methods as $method)
			{
				$permission = $controller . '/' . $method;
				$data = array(
					'name'			=> $permission,
					'permission'	=> $permission,
				);
				
				// Kiem tra da co trong csdl hay chua
				if($this->model_permission->get_total(array(
					'permission'=> $permission
				))==0)
				{
					$this->model_permission->create($data);
				}
			}
		echo json_encode(array(
			'state'	=> 'success',
			'title'	=> lang('notice'),
			'message'	=> lang('update_success'),
		));
	}
        public function get_all()
        {
            echo json_encode(array(
                'state' => 'success',
                'permissions'  => $this->model_permission->get_list(array(
					'select'	=> array('id','name'),
				))
            ));
        }
}

?>
